/**
 * Created by mj on 2017/3/30.
 */
$(function () {
    //初始化用户列表
    $('#dglist').datagrid({
        url: "queryAll",
        method: "post",
        title: "计划总表",
        striped: true,
        loadMsg: "努力加载中...",
        rownumbers: true,
        singleSelect: true,
        fit: true,
        fitColumns: true,//是否自动填充适合屏幕
        pagination: true,//是否显示底部分页工具栏
        pageSize: 15,
        pageList: [15, 20, 25],
        showFooter: true,
        autoRowHeight: false,
        columns: [[
            {field: 'partName', title: '零件名称', sortable: true, align: "center"},
            {field: 'textureOfMaterial', title: '材质', sortable: true, align: "center"},
            {field: 'specificationOfParts', title: '零件规格', sortable: true, width: 100, align: "center"},
            {field: 'drawingNumber', title: '图纸编号', sortable: true, align: "center"},
            {field: 'number', title: '数量', sortable: true, align: "center"},
            // {field: 'company', title: '单位', sortable: true,  align: "center"},
            // {field: 'purchase', title: '采购', sortable: true,  align: "center"},
            {field: 'sawingMachine', title: '锯床', sortable: true, align: "center"},
            {field: 'lathe', title: '车床', sortable: true, align: "center"},
            {field: 'millingMachine', title: '铣床', sortable: true, align: "center"},
            {field: 'cNC', title: 'CNC', sortable: true, align: "center"},
            {field: 'drillingMachine', title: '钻床', sortable: true, align: "center"},
            {field: 'tapping', title: '攻牙', sortable: true, align: "center"},
            {field: 'lineCutting', title: '线切割', sortable: true, align: "center"},
            {field: 'welding', title: '焊接', sortable: true, align: "center"},
            {field: 'grinding', title: '打磨', sortable: true, align: "center"},
            {field: 'totalWorkingHours', title: '总工时', sortable: true, align: "center"},
            {field: 'remainingWorkingHours', title: '剩余工时', sortable: true, align: "center"},
            {field: 'outsourcingSurfaceTreatmentProcess', title: '外协状态', sortable: true, align: "center"},
            // {field: 'processingState', title: '加工状态', sortable: true,  align: "center"},
            // {field: 'degreeOfCompletion', title: '完成度%', sortable: true,  align: "center"},
            {field: 'seizedGoods', title: '品检', sortable: true, align: "center"}
            // {field: 'plannedDeliveryDate', title: '交货日期', sortable: true,  align: "center"}
            // {field: 'remarks', title: '备注', sortable: true,  align: "center"}
        ]],
        toolbar: [
            {
                text: '增加', iconCls: 'icon-add', handler: function () {
                openWin("dglist", "addWin", "添加计划", 400, 400, "addForm", "save");
            }
            }, '-',
            // {
            //     text: '编辑', iconCls: 'icon-edit', handler: function () {
            //     editUser();
            // }
            // }, '-',
            // {
            //     text: '删除', iconCls: 'icon-remove', handler: function () {
            //     deleteUser();
            // }
            // }, '-',
            {
                text: '查看', iconCls: 'icon-more', handler: function () {
                openImg()
            }
            }, '-',
            {
                text: '刷新', iconCls: 'icon-reload', handler: function () {
                $('#dglist').datagrid("reload");
            }
            }, '-',
            {
                text: '启动/暂停', iconCls: 'icon-lock', handler: function () {
                changeStatus();
            }
            },
            '-'],
    });
    $('#dglist').datagrid('getPager').pagination({//分页栏下方文字显示
        beforePageText: '第',
        afterPageText: '页，总共 {pages}页',
        displayMsg: '当前显示从第{from}条到{to}条 共{total}条记录',
    });
});

function save(listId, winId, formId, flag) {
    if ($("#" + formId).form("validate")) {
        var form = $("#" + formId)[0];
        var formData = new FormData(form);
        $.messager.confirm("提醒", "您确认操作吗？", function (r) {
            if (r) {
                $.ajax({
                    url: "add",
                    data: formData,
                    type: "post",
                    dataType: "json",
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        if (data.msg == "success") {
                            $.messager.alert("提示", "操作成功！", "info", function () {
                                $("#" + listId).datagrid('reload');
                                $("#" + winId).window("close");
                                $("#" + formId).form("reset");
                                $("#editJisForm").form("reset");
                            });
                        } else {
                            $.messager.alert("警告", "操作失败！", "error", function () {
                                window.location.reload();
                            })
                        }
                    },
                    error: function (data) {
                        //alert("s")
                    },
                })
            }
        })
    }
}

//删除，可批量删除
function deleteUser() {
    var ids = $.getManyRecords("dglist")
    $.messager.confirm('确认', '您确认想要删除记录吗？', function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: "delete",
                data: {ids: ids},
                timeout: 20000,
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.msg == "success") {
                        $.messager.alert("提示", "删除成功！", "info", function () {
                            window.location.reload();
                        })
                    } else {
                        $.messager.alert("提示", "删除失败！", "info", function () {
                            window.location.reload();
                        });
                    }

                },
                error: function () {
                    $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
                }
            })
        }
    });
}

//修改用户信息
function editUser() {
    if (!$.getSingleRecord("dglist")) {
        return;
    }
    var id = $.getSingleRecord("dglist").id;
    window.location.href = "toUpdate?id=" + id;
}

/**
 * 改变用户状态
 */
function changeStatus() {
    if (!$.getSingleRecord("dglist")) {
        return;
    }
    var id = $.getSingleRecord("dglist").drawingNumber;
    $.messager.confirm('确认', '您确认要改变该用户的状态吗？', function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: "changeStatus",
                data: {id: id},
                timeout: 20000,
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.msg == "success") {
                        $.messager.alert("提示", "状态改变成功！", "info", function () {
                            $('#dglist').datagrid("reload");
                        })
                    } else {
                        $.messager.alert("提示", "操作失败！", "error", function () {
                            $('#dglist').datagrid("reload");
                        })
                    }
                }
            })
        }
    });
}

function openImg() {
    var curWwwPath = window.document.location.href;
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    var localhostPath = curWwwPath.substring(0, pos);
    // alert(localhostPath);
    var deploymentId = 25001;
    if (!$.getSingleRecord("dglist")) {
        return;
    }
    var deploymentKey = $.getSingleRecord("dglist").drawingNumber;
    // alert(deploymentKey);
    // $("#detailWin").show().window("open");
    $("#detailWin").html("<span style='display:table-cell; vertical-align:middle;'>" +
        "<img style=\"margin: 0 auto\" src=" + localhostPath + "/processView/viewByDeploymentKey/" + deploymentKey + "/>" +
        "</span>");
    $("#detailWin").window('open')
}
